define(["jquery", "Utils", "String"], function ($, Utils, String) {
    var __data_set__ = [];

    function DataSet() {

    }

    DataSet.FLAG = "edit___flag";
    DataSet.ID = "dataset___id";

    DataSet.ADD = "ADD";
    DataSet.EDIT = "EDIT";
    DataSet.REMOVE = "REMOVE";

    DataSet.prototype = {
        _check0: function (value) {
            if (!Utils.isObject(value)) {
                throw 'xxxx';
            }
        },
        _check: function (values) {
            if (Utils.isArray(values)) {
                var self = this;
                $.each(values, function (index, value) {
                    self._check0(value);
                });
                return;
            }
            this._check0(values);
        },
        _remove: function (id) {
            for (var i = 0; i < __data_set__.length; i++) {

                var item = __data_set__[i];
                if (item.__dataset_id__ == id) {
                    __data_set__.splice(index, index + 1);
                    break;
                }
            }
        },
        _handle: function (values) {
            var self = this;

            if (Utils.isArray(values)) {
                $.each(values, function (index, value) {
                    self._handle(value);
                });
                return;
            }

            if (Utils.isObject(values)) {
                var datas = __data_set__;
                if (String.isEmpty(datas)) {
                    if (!String.isEmpty(values.dataset___id)) {
                        throw '不存在可以编辑的数据。';
                    }

                    /*if (values[DataSet.FLAG] != DataSet.ADD) {
                        throw '不存在可以编辑的数据。';
                    }*/

                    values[DataSet.ID] = String.GUID();
                    values[DataSet.FLAG] = DataSet.ADD;
                    __data_set__.push(values);
                    return;
                }

                $.each(datas, function (index, data) {

                    if (values.__dataset_id__ == undefined) {
                        values[DataSet.ID] = String.GUID();
                        values[DataSet.FLAG] = DataSet.ADD;
                        __data_set__.push(values);
                        return ;
                    }

                    if (values.__dataset_id__ == data.__dataset_id__) {
                        if (values.__edit_flag__ == undefined) {
                            throw String.format('针对数据:ID[{0}]没有明确的操作类型', values.__dataset_id__);
                            // values[DataSet.FLAG] = DataSet.EDIT;
                        }

                        if ((!values[DataSet.FLAG] == REMOVE || values[DataSet.FLAG] == DataSet.EDIT)) {
                            throw String.format('针对数据:ID[{0}]使用非法的操作类型 {1}'
                                , values.__dataset_id__
                                , values[DataSet.FLAG]);
                        }

                        if (values[DataSet.FLAG] == REMOVE) {
                            //remove
                            self._remove(values.__dataset_id__);
                            return;
                        }
                        __data_set__[index] = values;
                    }

                });

                return;
            }
        },
        _handle_all:function(values,callback){

            if (Utils.isObject(values)) {
                var datas = __data_set__;
                if (String.isEmpty(datas)) {
                    if (!String.isEmpty(values.dataset___id)) {
                        throw '不存在可以编辑的数据。';
                    }

                    $.each(values, function (index, value) {
                        if(String.isEmpty(value.id)){
                            value[DataSet.ID] = String.GUID();
                            value[DataSet.FLAG] = DataSet.ADD;
                        }
                        if(!String.isEmpty(value.id)){
                            value[DataSet.ID] = value.dataset___id;
                            value[DataSet.FLAG] = DataSet.EDIT;
                        }
                        __data_set__.push(value);
                    });
                    //回调
                    callback(__data_set__,true);
                    return;
                }
            }
        },
        _handle_single: function (values,type,callback) {
            if(!Utils.isObject(values)){
                return false;
            }

            var datas = __data_set__;

            if(type == DataSet.ADD){//新增
                $.each(values, function (index, value) {
                    if(String.isEmpty(value.id)){
                        value[DataSet.ID] = String.GUID();
                        value[DataSet.FLAG] = DataSet.ADD;
                    }
                    if(!String.isEmpty(value.id)){
                        value[DataSet.ID] = value.dataset___id;
                        value[DataSet.FLAG] = DataSet.EDIT;
                    }
                    __data_set__.push(value);
                });
                callback(__data_set__,true);
                return;
            }

            if(type == DataSet.EDIT){//编辑
                $.each(values, function (index1, value) {
                    $.each(datas, function (index, data) {
                        if(data.dataset___id == value.dataset___id){
                            value[DataSet.ID] = value.dataset___id;
                            value[DataSet.FLAG] = DataSet.EDIT;
                            __data_set__[index] = value;
                            return false;
                        }
                    });
                });
                callback(__data_set__,true);
                return;
            }

            if(type == DataSet.REMOVE){//删除
                var __delete__data_set__ = [];

                $.each(values, function (index1, value) {
                    $.each(datas, function (index, data) {
                        if(data.dataset___id == value.dataset___id){
                            data[DataSet.FLAG] = DataSet.REMOVE;
                            __data_set__[index] = value;
                        }
                        if(data.dataset___id != value.dataset___id && data.edit___flag != DataSet.REMOVE){
                            __delete__data_set__.push(data);
                        }
                    });
                });
                callback(__delete__data_set__,true);
                return;
            }

        },

        handle: function (values) {
            this._check(values);

            this._handle(values);
        },
        handle_single: function (values,type,callback) {//单条数据改变
            this._check(values);

            this._handle_single(values,type,callback);
        },
        handle_all: function(values,callback){
            this._check(values);

            this._handle_all(values,callback);
        },
        getData: function () {
            return __data_set__;
        }
    };

    DataSet.getInstance = function () {
        return new DataSet();
    };

    return DataSet;

});